<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Project extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if(!session('name')){
            $this->redirect("/admin/login/login");
        }
    }
    protected $project;
    public function initialize()
    {
        parent::initialize();
        $this->project=new \app\common\model\Project();
    }

    public function index(){
        $proList=$this->project->getProjectList();
        $this->assign("proList",$proList);
        return $this->fetch();
    }
    public function add(){
        if(request()->isPost()){
            $data=input('param.');
            $checkData=new \app\admin\Validate\Project();
            if(!$checkData->check($data)){
                return $this->error($checkData->getError());
            }else{
                $save=$this->project->saveProject($data);
                if($save['valid']){
                    return $this->success($save['msg'],'/admin/project/index');
                }else{
                    return $this->error($save['msg']);
                }
            }
        }
        return $this->fetch();
    }
    public function edit(){
        if(request()->isPost()){
            $data=input('param.');
            $checkData=new \app\admin\Validate\Project();
            if(!$checkData->check($data)){
                return $this->error($checkData->getError());
            }else{
                $save=$this->project->editProject($data);
                if($save['valid']){
                    return $this->success($save['msg'],'/admin/project/index');
                }else{
                    return $this->error($save['msg']);
                }
            }
        }
        $project=$this->project->getProject(input('param.id'));
        $this->assign("project",$project);
        return $this->fetch();
    }
    public function delete(){
        $id=input('param.id');
        $del=$this->project->where('pro_id',$id)->delete($id);
        if($del){
            return json(["msg"=>1]);
        }else{
            return json(["msg"=>0]);
        }
    }
}
